
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author LONGBK
 */
public class StockMap {
    private Image tudo1IMG, tudo2IMG;
    private Sprite tudo1, tudo12, tudo2;
    private Image cars3IMG;
    private Image nhakhoIMG;
    private Image caycanhIMG;
    private Image paperIMG;
    private Image bookIMG;
    private Image darkBorderIMG;
    private Image dentoIMG;
    private Image bangdientuIMG;
    private Image ketsatIMG;
    private Image manoquinIMG;
    private Image buttonIMG;
    private Sprite button;
    private Sprite manoquin;
    private Sprite bangdientu;
    private Sprite dento;
    private Vector light;
    private Sprite book;
    private Sprite paper;
    private Sprite ketsat;
    private Sprite caycanh;
    private TiledLayer darkBorder;
    private TiledLayer map;
    private TiledLayer darkBorder2;
    private Vector darkBorder2V;
    private Sprite hieuung;
    private int size;
    private int[][]mapXY = new int[20][3];
    private int[][] tilesDarkDeleteByLight;
    private int[][] lightPos;
    private boolean [] lightToTop = new boolean[3];
    private int [][] walkArray;
    private int[][] tiles;
    public String[][] character;
    public boolean [][] paintCharacter = new boolean[4][9];
    private boolean [][] fixStateCell = new boolean[26][32];
    private int[][] tilesTemp = new int[26][32];
    private String[] string;
    private String talk;
    public  boolean delay;
    public boolean open;
    private byte[] textT;
    private int endTextID;
    private boolean isDrawing;
    public int nextFrame;
    private int row_mr, col_mr;
    public boolean showBook, showPaper;
    private boolean[] collidesWithLight = new boolean[5];
    private int maxCol, minCol, maxRow, minRow;
    public boolean boardApp;
    public boolean textForBoard;
    public int char_row, char_col;
    public String stringCp = "";
    public boolean isCorrect;
    public boolean drawOutputText;
    public String output = "";
    private boolean isDelBorder;
    private int width;
    private int height;
    public boolean useNC;
    public boolean useKey;
    public boolean nextString;
    public boolean endString;
    private int mrLX, mrLY;
    public boolean openDoor;
    private int roll;
    private int timeRoll;
    private int numItem;
    public StockMap(){


    }
    public void loadMap(SunnetGameCanvas gameCanvas){
        width = gameCanvas.getWidth();
        height = gameCanvas.getHeight();
        numItem = 0;
        darkBorder2V = new Vector(3);
        tiles = new int[][]{
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 10, 10, 11, 0, 0, 9, 10, 10, 11, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 28, 56, 56, 56, 56, 56, 56, 56, 56, 56, 58, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 2, 4, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 39, 56, 56, 56, 56, 56, 56, 56, 56, 56, 54, 0, 14, 14, 14, 14, 0, 5, 17, 18, 18, 19, 1, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 39, 56, 56, 43, 44, 45, 56, 21, 29, 30, 54, 0, 25, 25, 25, 25, 0, 5, 17, 18, 18, 19, 1, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 39, 56, 56, 55, 55, 55, 56, 57, 29, 30, 54, 0, 0, 0, 0, 0, 0, 5, 17, 18, 18, 19, 1, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 39, 56, 56, 56, 56, 56, 56, 57, 29, 30, 54, 0, 0, 40, 41, 0, 0, 5, 17, 18, 18, 19, 1, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 50, 56, 56, 43, 44, 45, 56, 57, 29, 30, 26, 56, 28, 51, 52, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 0, 0, 0 },
                { 0, 0, 0, 55, 56, 56, 55, 55, 55, 56, 57, 29, 30, 26, 56, 39, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 0, 0, 0 },
                { 0, 0, 0, 56, 56, 56, 56, 56, 56, 56, 57, 29, 30, 26, 56, 39, 56, 43, 44, 45, 56, 56, 56, 56, 56, 56, 56, 56, 56, 0, 0, 0 },
                { 0, 0, 0, 56, 56, 56, 56, 56, 56, 56, 57, 29, 30, 26, 56, 39, 56, 55, 55, 55, 56, 56, 56, 56, 56, 56, 56, 56, 56, 0, 0, 0 },
                { 0, 0, 0, 56, 56, 56, 56, 56, 56, 56, 57, 29, 30, 26, 56, 39, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 0, 0, 0 },
                { 0, 0, 0, 56, 56, 56, 56, 56, 56, 56, 57, 29, 30, 26, 56, 50, 56, 43, 44, 45, 56, 56, 56, 56, 56, 56, 56, 56, 20, 0, 0, 0 },
                { 0, 0, 0, 56, 56, 56, 56, 56, 56, 56, 57, 29, 30, 26, 56, 55, 56, 55, 55, 55, 56, 56, 56, 56, 56, 56, 56, 56, 31, 0, 0, 0 },
                { 0, 0, 0, 56, 56, 56, 56, 56, 56, 56, 57, 29, 30, 26, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 0, 0, 0 },
                { 0, 0, 0, 56, 56, 56, 56, 56, 56, 56, 57, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 0, 0, 0 },
                { 0, 0, 0, 56, 56, 56, 56, 56, 56, 56, 57, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 0, 0, 0 },
                { 0, 0, 0, 7, 7, 7, 7, 7, 7, 7, 34, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 0, 0, 0 },
                { 0, 0, 0, 26, 56, 56, 56, 26, 56, 56, 57, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 0, 0, 0 },
                { 0, 0, 0, 27, 27, 27, 27, 27, 27, 27, 13, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 0, 0, 0 },
                { 0, 0, 0, 27, 27, 27, 27, 27, 27, 27, 13, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 0, 0, 0 },
                { 0, 0, 0, 27, 27, 27, 27, 27, 27, 27, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 27, 27, 27, 27, 27, 27, 27, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 27, 27, 27, 27, 27, 27, 27, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
            };
        
        tilesDarkDeleteByLight = new int[][]{
            
                { 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3 },
                { 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6 },
                { 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6 },
                { 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6 },
                { 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6 },
                { 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6 },
                { 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6 },
                { 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6 },
                { 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9 }
        };
        light = new Vector(3);
        lightPos = new int[][]{{8, 26},
                               {18, 12},
                               {6, 6}};

        character = new String[][]{{"A", "B", "C", "D", "E", "F", "G", "H", "I"},
                                    {"J", "K", "L", "M", "N", "0", "P", "Q", "R"},
                                    {"S", "T", "U", "V", "W", "X", "Y", "Z"}};
        string = new String[]{"Nước hoa dùng gây mê!", "Chắc chắn trong này có j đó!", "Chất đặc biệt.", "Nhìn xuyên thấu kết hợp với một chất đặc biệt.", "Chẳng lẽ đây là loại thắt lưng đặc biệt có tác dụng như dao ? Làm thế nào để","nhận ra đc nó đây? A, nó làm bằng kim loại!",
                             "Chắc chắn chìa khóa phải để ngay trong nhà kho.", "Một cuộn giấy!","Sao lại là 24?", "Số 24 xuất hiện trong rất nhiều dãy số","Những cái tên có số 24? Liệu có bí ẩn j chăng?",
                             "Két sắt đây rồi!Chắc tập hồ sơ nằm trong này.","Số 24, những cái tên, dãy số? Alex nói rằng mật mã chỉ có 3 kí tự…", "Tập hồ sơ đây rồi!"};
                           

        String[] info = new String[]{"+ Hắn thích đến các quán bar",
                                     "+ Không thích hút thuốc lá",
                                     "+ Thích đội mũ cảnh sát",
                                     "+ Mặc vest",
                                     "+ Thích màu …",
                                     "+ Thuận tay trái…."
                                    };
        walkArray = new int[][]{{ 1, 2, 2, 3 },
                                    { 4, 0, 0, 6 },
                                    { 4, 0, 0, 6 },
                                    { 4, 0, 0, 6 },
                                    { 7, 8, 8, 9 }};

        char_row = 0; char_col = 0;
        InputStream is = getClass().getResourceAsStream("/mapnhakho.txt");
        int [] memoChars = new int[5];
        int chars;
        int i = 0;
        int j = 0;
        int charLength = 0;

        try{
            while((chars = is.read())!=-1){
                if(chars!=32 && chars != 10){
                    memoChars[charLength] = chars - 48;
                    charLength ++;
                }
                else if(chars == 32){
                    if(charLength == 1)
                        mapXY[i][j] = memoChars[0];    
                    else
                        mapXY[i][j] = memoChars[0] * 10 + memoChars[1];
                    charLength = 0;
                    j ++;
                }
                else{
                    mapXY[i][j] = memoChars[0];
                    charLength = 0;
                    j = 0;
                    i ++;
                }
            }   
            size = i;
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            nhakhoIMG = Image.createImage("/socanhsat.png");
            tudo1IMG = Image.createImage("/tudo1.png");
            tudo2IMG = Image.createImage("/tudo2.png");
            dentoIMG = Image.createImage("/dento.png");
            paperIMG = Image.createImage("/paper.png");
            bookIMG = Image.createImage("/book.png");
            caycanhIMG = Image.createImage("/caycanh.png");
            darkBorderIMG = Image.createImage("/darkBorder.png");
            bangdientuIMG = Image.createImage("/bangdientu.png");
            cars3IMG = Image.createImage("/cars3.png");
            ketsatIMG = Image.createImage("/ket.png");
            manoquinIMG = Image.createImage("/manoquin.png");
            buttonIMG = Image.createImage("/button.png");
        } catch (Exception e) {
            e.printStackTrace();
        }

        map = new TiledLayer(32, 26, nhakhoIMG, 16, 16);
        darkBorder = new TiledLayer(10, 10, darkBorderIMG, 16, 16);
        for(int index = 0; index < 3; index ++){
            darkBorder2 = new TiledLayer(20, 20, darkBorderIMG, 16, 16);
            darkBorder2V.addElement(darkBorder2);
        }
        tudo1= new Sprite(tudo1IMG, 16, 25);
        tudo1.setFrameSequence(new int[]{0, 1, 2, 3, 4, 5, 6});
        tudo1.setFrame(0);
        tudo12 = new Sprite(tudo1IMG, 16, 25);
        tudo2 = new Sprite(tudo2IMG, 25, 27);
        tudo2.setFrameSequence(new int[]{0, 1, 2, 3, 4, 5, 6});
        tudo2.setFrame(0);
        tudo1.setPosition(4 * 16, 5 * 16 - tudo1.getHeight());
        tudo12.setPosition(6 * 16, 5 * 16 - tudo1.getHeight());
        tudo2.setPosition(18 * 16, 10 *16 - tudo2.getHeight());
        paper = new Sprite(paperIMG, 50, 40);
        paper.setFrameSequence(new int[]{0, 1, 2, 3, 4});
        book = new Sprite(bookIMG, 120, 120);
        book.setFrameSequence(new int[]{0, 1, 2, 3, 4, 5, 6 ,7});
        book.setPosition(215, 18);
        bangdientu = new Sprite(bangdientuIMG, 120, 120);
        caycanh = new Sprite(caycanhIMG, 16, 41);
        caycanh.setPosition(5 * 16, 5 * 16 - caycanh.getHeight());
        ketsat = new Sprite(ketsatIMG, 21, 45);
        ketsat.setFrameSequence(new int[]{0, 1, 2, 3, 4});
        ketsat.setPosition(29 * 16 - ketsat.getWidth() , 10 * 16 - ketsat.getHeight());
        manoquin = new Sprite(manoquinIMG,16 , 31);
        manoquin.setPosition(3 * 16, 9 * 16);
        manoquin.setFrameSequence(new int[]{0, 1});
        button = new Sprite(buttonIMG, 8, 8);
        button.setFrameSequence(new int[]{0, 1});
        button.setFrame(0);
        button.setPosition(26 * 16 + 4, 6 * 16 + 4);
        /* Set cac thuoc tinh*/
        gameCanvas.getLayerManager().append(book);
        gameCanvas.getMrL().setPosition(21*16  , 9*16);
        //gameCanvas.getMrL().setPosition(9*16 , 6*16);
        gameCanvas.getMrL().setFrame(0);
        gameCanvas.getLayerManager().append(gameCanvas.getMrL());
        for(int index = 0; index < 3; index ++){
            dento = new Sprite(dentoIMG , 34, 48);
            dento.setFrameSequence(new int[]{0, 1});
            dento.setPosition(lightPos[index][1] * 16, lightPos[index][0]* 16);
            gameCanvas.getLayerManager().append(dento);
            dento.setVisible(false);
            light.addElement(dento);
        }
        hieuung = new Sprite(gameCanvas.getHieungIMG(), 35, 35);
        hieuung.setFrameSequence(new int[]{0, 0, 1, 1, 2, 2});
        gameCanvas.getLayerManager().append(tudo1);
        gameCanvas.getLayerManager().append(tudo2);
        gameCanvas.getLayerManager().append(tudo12);
        gameCanvas.getLayerManager().append(caycanh);
        gameCanvas.getLayerManager().append(ketsat);
        gameCanvas.getLayerManager().append(manoquin);
        gameCanvas.getLayerManager().append(button);
        manoquin.setVisible(false);
        tudo1.setVisible(false);
        tudo12.setVisible(false);
        tudo2.setVisible(false);
        book.setVisible(false);
        caycanh.setVisible(false);
        ketsat.setVisible(false);
        button.setVisible(false);
        gameCanvas.getLayerManager().append(darkBorder);
        map.setPosition(0,  0);
        gameCanvas.getLayerManager().append(map);
    }
    public Sprite getBook(){
        if(book != null)
            return this.book;
        else
            return null;
    }
    public Sprite getPaper(){
        if(paper != null)
            return this.paper;
        else
            return null;
    }
    public void getColides(SunnetGameCanvas gameCanvas){
        mrLX = gameCanvas.getMrL().getX();
        mrLY = gameCanvas.getMrL().getY();
        if(/*mrLY <= 9 * 16  -  gameCanvas.getMrL().getHeight() + 2*/ mrLY == 8 * 16 && mrLX > 13 * 16 && mrLX < 28 * 16){
            if(gameCanvas.press2 || gameCanvas.key2){
                //if(mrLY == 7 * 16 && mrLX > 26 * 16 - 8 && mrLX < 27 * 16 + 8)
                if(fixStateCell[6][26] &&  numItem == 11){
                    openDoor = true;
                }
            }
            gameCanvas.key2 = false;   
            gameCanvas.press2 = false;
        }        
        if(/*mrLY <= 4 * 16 - gameCanvas.getMrL().getHeight() + 2*/ mrLY == 3 * 16 && mrLX < 13 * 16){
            gameCanvas.key2 = false;
            gameCanvas.press2 = false;
        }
        if(mrLX <= 3 * 16){
            gameCanvas.key4 = false;
            gameCanvas.press4 = false;
        }
        if(mrLX >= 28 * 16){
            gameCanvas.key6 = false;
            gameCanvas.press6 = false;
        }
        if(mrLY >= 23 * 16 - gameCanvas.getMrL().getHeight() ){
            gameCanvas.key8 = false;
            gameCanvas.press8 = false;
        }      
        for(int i = 0; i < size; i ++){
            if(mapXY[i][2] == 1){
                if((mrLX == mapXY[i][1]*16 - gameCanvas.getMrL().getWidth())&&(mrLY>mapXY[i][0]*16 - gameCanvas.getMrL().getHeight() && mrLY<mapXY[i][0]*16 + 6 * 16 + 14 - gameCanvas.getMrL().getHeight())){
                    gameCanvas.key6 = false;
                    gameCanvas.press6 = false;
                }
                // nuoc hoa
                if((mrLX == (mapXY[i][1] + 1)*16)&&(mrLY>mapXY[i][0]*16 - gameCanvas.getMrL().getHeight() && mrLY<(mapXY[i][0] + 6) *16 + 16 - gameCanvas.getMrL().getHeight())){
                    if(gameCanvas.press4 || gameCanvas.key4){
                        if(mrLY > 4 * 16  && mrLY < 5 * 16 + 8){
                            if(!gameCanvas.updateToBag[7]){
                                gameCanvas.updateToTuido(7);
                                talk = string[0];
                                delay =  true;
                                numItem ++;
                            }
                        }
                        
                    }
                    if(mrLY > 5 * 16 + 8 && mrLY < 8 * 16 + 8){
                        useNC = true;
                        if((gameCanvas.press4 || gameCanvas.key4)){
                            if(!gameCanvas.useItem[11]){
                                if(!nextString){
                                    talk = string[4];
                                    delay =  true;
                                    endString = false;
                                }
                            }
                            else{
                                if(!gameCanvas.updateToBag[5]){
                                    gameCanvas.updateToTuido(5);
                                    numItem ++;
                                }
                            }
                        }
                        if(!gameCanvas.useItem[11]){
                            if(endTextID == string[4].length()){
                                if(!nextString && !endString){
                                    nextString = true;
                                    talk = string[5];
                                    delay = true;    
                                    isDrawing = false;    
                                }
                            }
                        }
                    }
                    gameCanvas.key4 = false;
                    gameCanvas.press4 = false;      
                }

                if(mrLX>mapXY[i][1]*16 - gameCanvas.getMrL().getWidth() &&mrLX < (mapXY[i][1]+1)*16 && (mrLY== ((mapXY[i][0] + 7) *16) - gameCanvas.getMrL().getHeight())){
                    gameCanvas.key2 = false;
                    gameCanvas.press2 = false;      
                }
                if(mrLX>mapXY[i][1]*16 - gameCanvas.getMrL().getWidth() &&mrLX<(mapXY[i][1]+1)*16 && mrLY== mapXY[i][0]*16 - gameCanvas.getMrL().getHeight()){
                    gameCanvas.key8 = false;
                    gameCanvas.press8 = false;      
                }
            }
            if(mapXY[i][2] == 2){
                if(mrLX == mapXY[i][1]*16 - gameCanvas.getMrL().getWidth() && mrLY >mapXY[i][0] * 16 + 4 - gameCanvas.getMrL().getHeight() && mrLY < (mapXY[i][0] + 1) * 16 + 14 - gameCanvas.getMrL().getHeight()){
                    gameCanvas.key6 = false;
                    gameCanvas.press6 = false;
                }
                if(mrLX == (mapXY[i][1] + 3)*16 && mrLY >mapXY[i][0] * 16 + 4 - gameCanvas.getMrL().getHeight() && mrLY < (mapXY[i][0] + 1) * 16 + 14 - gameCanvas.getMrL().getHeight()){
                    gameCanvas.key4 = false;
                    gameCanvas.press4 = false;
                }
                if(mrLX>mapXY[i][1]*16 - gameCanvas.getMrL().getWidth() && mrLX<(mapXY[i][1] + 3)*16 && mrLY == (mapXY[i][0] + 2) * 16 - gameCanvas.getMrL().getHeight()){
                    gameCanvas.key2 = false;
                    gameCanvas.press2 = false;
                }
                if(mrLX>mapXY[i][1]*16 - gameCanvas.getMrL().getWidth() && mrLX<(mapXY[i][1] + 3)*16 && mrLY == mapXY[i][0] * 16 + 4 - gameCanvas.getMrL().getHeight()){
                    gameCanvas.key8 = false;
                    gameCanvas.press8 = false;
                }
            }
            if(mapXY[i][2] == 3){
                if(mrLX > mapXY[i][1] * 16 - gameCanvas.getMrL().getWidth() && mrLX < (mapXY[i][1] + 2) * 16 && mrLY == (mapXY[i][0] + 2)*16 - gameCanvas.getMrL().getHeight()){
                    gameCanvas.key2 = false;
                    gameCanvas.press2 = false;
                }
                if(mrLX == (mapXY[i][1] + 2) * 16 && mrLY< (mapXY[i][0] + 2)*16 - gameCanvas.getMrL().getHeight() && mrLY > mapXY[i][0]*16 - gameCanvas.getMrL().getHeight()){
                    gameCanvas.key4 = false;
                    gameCanvas.press4 = false;
                }
            }
            if(mapXY[i][2] == 4){
                if((mrLX == mapXY[i][1]*16 - gameCanvas.getMrL().getWidth())&&mrLY>mapXY[i][0]*16 - gameCanvas.getMrL().getHeight() + 6 && mrLY<(mapXY[i][0] + 2)*16 - gameCanvas.getMrL().getHeight() + 4){
                    gameCanvas.key6 = false;
                    gameCanvas.press6 = false;
                }
                if(mrLX > mapXY[i][1]*16 - gameCanvas.getMrL().getWidth() && (mrLY== (mapXY[i][0] + 2)*16- gameCanvas.getMrL().getHeight() + 4)){
                    if(gameCanvas.press2 || gameCanvas.key2){
                        if(!gameCanvas.updateToBag[9]){
                            gameCanvas.updateToTuido(9);
                            numItem ++;
                        }
                    }
                    gameCanvas.key2 = false;
                    gameCanvas.press2 = false;           
                }
                if(mrLX>mapXY[i][1]*16 - gameCanvas.getMrL().getWidth() && mrLY== 12 * 16 + 4){
                    if(gameCanvas.press8 || gameCanvas.key8){                 
                        if(!gameCanvas.updateToBag[9]){
                            gameCanvas.updateToTuido(9);
                            numItem ++;
                        }
                    }
                    gameCanvas.key8 = false;
                    gameCanvas.press8 = false;
                }
            }
            if(mapXY[i][2] == 5){
                if(mrLX == mapXY[i][1] * 16 - 4 && mrLY >= mapXY[i][0] * 16 - gameCanvas.getMrL().getHeight()){
                    gameCanvas.key6 = false;
                    gameCanvas.press6 = false;
                }
                if(mrLX == (mapXY[i][1] + 1)* 16 && mrLY>= mapXY[i][0] * 16 - gameCanvas.getMrL().getHeight()){
                    gameCanvas.key4 = false;
                    gameCanvas.press4 = false;
                }
            }
            if(mapXY[i][2] == 6){
                if(mrLX == mapXY[i][1] * 16 - gameCanvas.getMrL().getWidth() && mrLY <= (mapXY[i][0] + 13) * 16 - gameCanvas.getMrL().getHeight()){
                    gameCanvas.key6 = false;
                    gameCanvas.press6 = false;       
                }
                if(mrLX == mapXY[i][1]* 16  && mrLY<= (mapXY[i][0] + 13) * 16 - gameCanvas.getMrL().getHeight()){
                    gameCanvas.key4 = false;
                    gameCanvas.press4 = false;
                }
            }
            if(mapXY[i][2] == 7){
                if((mrLX>= mapXY[i][1]*16 - gameCanvas.getMrL().getWidth()) && mrLX <= (mapXY[i][1] + 8)*16 - gameCanvas.getMrL().getWidth() && (mrLY == mapXY[i][0] * 16 - 20)){
                    gameCanvas.key8 = false;
                    gameCanvas.press8 = false;
                }
            }
        }
        // Collides with den
        for(int i = 0; i < lightPos.length; i ++){
            if(mrLX == lightPos[i][1] * 16 && mrLY > (lightPos[i][0] + 1) * 16 - 8 && mrLY < (lightPos[i][0] + 1) * 16 + 4){
                gameCanvas.key6 = false;
                gameCanvas.press6  = false;
                dento = (Sprite)light.elementAt(i);
                dento.setFrame(1);
                collidesWithLight[i] = true;
            }
            if(mrLX == (lightPos[i][1] + 1) * 16  && mrLY > (lightPos[i][0] + 1 )* 16 - 8 && mrLY < (lightPos[i][0] + 1) * 16 + 4){
                gameCanvas.key4 = false;
                gameCanvas.press4 = false;
                dento = (Sprite)light.elementAt(i);
                dento.setFrame(1);
                collidesWithLight[i] = true;
            }
            if(mrLX > lightPos[i][1] * 16 && mrLX < (lightPos[i][1] + 1) * 16 && mrLY == (lightPos[i][0] + 1) * 16  + 4){
                gameCanvas.key2 = false;
                gameCanvas.press2 = false;
                dento = (Sprite)light.elementAt(i);
                dento.setFrame(1);
                collidesWithLight[i] = true;
            }
            if(mrLX > lightPos[i][1]* 16  && mrLX < (lightPos[i][1] + 1)* 16 && mrLY == (lightPos[i][0] + 1) * 16 - 8){
                gameCanvas.key8 = false;
                gameCanvas.press8 = false;
                dento = (Sprite)light.elementAt(i);
                dento.setFrame(1);             
                collidesWithLight[i] = true;

            }
        }
        //update layer light
        for(int i = 0; i < lightPos.length; i ++){
            if(mrLX > (lightPos[i][1] - 1) * 16 && mrLX < (lightPos[i][1] +  2) * 16){
                if(mrLY <= (lightPos[i][0] + 1)* 16 - 8){
                    if(!lightToTop[i]){
                
                        dento = (Sprite) light.elementAt(i);
                        gameCanvas.getLayerManager().insert(dento, 0);
                        lightToTop[i] = true;
                    }
                }
                if(mrLY > (lightPos[i][0] + 1) * 16 - 8){
                    if(lightToTop[i]){
                       
                        dento = (Sprite) light.elementAt(i);
                        gameCanvas.getLayerManager().insert(dento, 5 + i);
                        lightToTop[i] = false;
                    }
                }
            }
        }
        // cay
        if(mrLY == 4 * 16 && mrLX > caycanh.getX() - 8 && mrLX < caycanh.getX() + caycanh.getWidth()){
            gameCanvas.key2 = false;
            gameCanvas.press2 = false;
        }

        // Tu do 1
        if(mrLY == 4 * 16 && mrLX > tudo12.getX() - gameCanvas.getMrL().getWidth() && mrLX < tudo12.getX() + tudo12.getWidth()){
            
            if(gameCanvas.press2 || gameCanvas.key2){
               if((gameCanvas.key2 || gameCanvas.press2) && !gameCanvas.updateToBag[3]){
                    gameCanvas.updateToTuido(3);
                    talk = string[3];
                    delay = true;
                    numItem ++;
                    //System.out.println("son.......");
               }
            }
            gameCanvas.press2 = false;
            gameCanvas.key2 = false;
        }
        if(mrLX == tudo12.getX() + tudo12.getWidth() && mrLY < 4 * 16 ){
            gameCanvas.key4 = false;
            gameCanvas.press4 = false;           
        }
        if(mrLY == 4 * 16  && mrLX > tudo1.getX() - gameCanvas.getMrL().getWidth() && mrLX < tudo1.getX() + tudo1.getWidth()){
            if(tudo1.getFrame() == 0){
                if(gameCanvas.key2 || gameCanvas.press2){
                    delay = true;
                    talk = string[1];
                    //isDrawing = false;                   
                }
            }
            if(open){
                if(tudo1.getFrame() != 5){
                    if(nextFrame % 3 == 0)
                        tudo1.nextFrame();
                }
                if(tudo1.getFrame() == 5){
                    if(gameCanvas.key2 || gameCanvas.press2){
                        open = false;
                        delay = true;
                        talk = string[2];
                        isDrawing = false;
                    }
                }
            }                   
            gameCanvas.key2 = false;
            gameCanvas.press2 = false;
        }
        //tu do 2
        if(mrLY <= tudo2.getY() + tudo2.getWidth() - 24 && mrLX > tudo2.getX() - gameCanvas.getMrL().getWidth() && mrLX < tudo2.getX() + tudo2.getWidth() - 12){
            useKey = true;
            if(tudo2.getFrame() == 0){
                if(gameCanvas.key2 || gameCanvas.press2){
                    if(!gameCanvas.useItem[9]){
                        delay = true;
                        talk = string[6];
                    }
                    if(gameCanvas.useItem[9]){
                        if(tudo2.getFrame() == 0){
                            open = true;                                               
                            tudo2.nextFrame();
                        }
                    }
                }           
            }
            if(open){              
                if(tudo2.getFrame() != 5){
                    if(nextFrame % 3 == 0)
                        tudo2.nextFrame();
                }
                if(tudo2.getFrame() == 5){
                    if(gameCanvas.key2 || gameCanvas.press2){
                        tudo2.nextFrame();
                        gameCanvas.updateToTuido(10);
                        open = false;
                        delay = true;
                        talk = string[7];
                        isDrawing = false;
                        numItem ++;
                    }
                }
            }
            gameCanvas.key2 = false;
            gameCanvas.press2 = false;  
        }
        if(mrLX == tudo2.getX() + tudo2.getWidth() - 13 && mrLY <  8 * 16 + 8 ){
            gameCanvas.key4 = false;
            gameCanvas.press4 = false;
        }
        //manoquin
        if(mrLY == 9 * 16 + 8 && mrLX < manoquin.getX() + manoquin.getWidth() - 8){
            if(gameCanvas.press2 || gameCanvas.press2){
                if(!gameCanvas.updateToBag[4] || !gameCanvas.updateToBag[6]){
                    gameCanvas.updateToTuido(4);
                    gameCanvas.updateToTuido(6);
                    numItem = numItem + 2;
                }
            }
            manoquin.setFrame(1);
            gameCanvas.key2 = false;
            gameCanvas.press2 = false;
        }

        //ket sat
         if(mrLY == ketsat.getY() + ketsat.getHeight() - 24 && mrLX > ketsat.getX() - gameCanvas.getMrL().getWidth()){
            if(ketsat.getFrame() == 0){
                if(gameCanvas.press2 || gameCanvas.key2){
                    if(!isCorrect){
                        if(!textForBoard){
                            talk = string[11];
                            delay = true;
                        }
                        else if(!boardApp){
                            talk = string[12];
                            delay = true;
                            boardApp = true;
                            gameCanvas.nextScence = true;
                            drawOutputText = true;
                            output = "ENTER KEY CODE";
                        }
                    }
                }
            }
            if(open && !boardApp && !gameCanvas.nextScence){
                if(ketsat.getFrame() != 3){
                    if(nextFrame % 3 == 0)
                        ketsat.nextFrame();
                }
                if(ketsat.getFrame() == 3){
                    if(gameCanvas.key2 || gameCanvas.press2){
                        open = false;
                        delay = true;
                        talk = string[13];
                        isDrawing = false;
                    }
                }
            }
            if((gameCanvas.press2 || gameCanvas.key2) && !gameCanvas.updateToBag[11]){
                gameCanvas.updateToTuido(11);
                numItem ++;
            }
            gameCanvas.key2 = false;
            gameCanvas.press2 = false;

        }
        if(mrLX == ketsat.getX() - gameCanvas.getMrL().getWidth() + 1 && mrLY <   8 * 16 + 8 ){
            gameCanvas.key6 = false;
            gameCanvas.press6 = false;
        }
        if(gameCanvas.getMrL().collidesWith(book, true) && !gameCanvas.updateToBag[8]){
            gameCanvas.getLayerManager().remove(book);
            gameCanvas.updateToTuido(8);
            numItem ++;
            
        }
        //Oto
        if(mrLX == 22 * 16 - gameCanvas.getMrL().getWidth() + 4 && mrLY >= 17 * 16 - gameCanvas.getMrL().getHeight() + 16){
            gameCanvas.key6 = false;
            gameCanvas.press6 = false;
        }
        if(mrLX == 26 * 16 - 4 && mrLY >= 17 * 16 - gameCanvas.getMrL().getHeight() + 16){
            gameCanvas.key4 = false;
            gameCanvas.press4 = false;
        }
        if(mrLX > 22 * 16 - gameCanvas.getMrL().getWidth() + 4 && mrLX < 26 * 16 - 4 && mrLY == 17 * 16 - gameCanvas.getMrL().getHeight() + 16){
            gameCanvas.key8 = false;
            gameCanvas.press8 = false;
        }
    }
    public void openDoorProcees(){     
        timeRoll = timeRoll + 1;
        if(timeRoll >= 5){
            if(roll < 3){
                roll ++;
            }
            timeRoll = 0;
        }
        for(int row = 0 ; row <= roll; row ++){
            for(int col = 0; col < 4; col ++){
                if(row == 0){
                    if(col == 0 || col == 3)
                        map.setCell(21 + col , 8 - row, 23);
                    if(col == 1 || col == 2)
                        map.setCell(21 + col, 8 - row, 24);
                }
                if(row == 1){
                    map.setCell(21 + col, 8 - row, 22);
                }
                if(row == 2){
                    if(col == 0)
                        map.setCell(21 + col , 8 - row, 36);
                    if(col == 1)
                        map.setCell(21 + col, 8 - row, 32);
                    if(col == 2)
                        map.setCell(21 + col, 8 - row, 33);
                    if(col == 3)
                        map.setCell(21 + col, 8 - row, 53);
                }
                if(row == 3){
                    if(col == 0)
                        map.setCell(21 + col , 8 - row, 12);
                    else
                        map.setCell(21 + col, 8 - row, 45 + col );
                    
                }
            }
        }
    }
    public void deleteDark(SunnetGameCanvas gameCanvas, Graphics g){
        System.out.println("col_mr:" + col_mr + "/row_mr:" + row_mr);
        for(int index = 0; index < 3; index ++){
            darkBorder2 = (TiledLayer)darkBorder2V.elementAt(index);
            for (int row = 0; row <= 8; row++) {
                for (int col = 0; col <= 12; col++){
                    darkBorder2.setCell(col, row, tilesDarkDeleteByLight[row][col]);
                }
            }
        }
        for(int i = 0; i < lightPos.length; i ++){
            if(collidesWithLight[i]){
                darkBorder2 = (TiledLayer)darkBorder2V.elementAt(i);
                if(lightPos[i][0] - 3 < 0)
                    minRow = 0;
                else
                    minRow = lightPos[i][0] - 3;
                if(lightPos[i][0] + 5 >= tiles.length)
                    maxRow = tiles.length - 1;
                else
                    maxRow = lightPos[i][0] + 5;
                if(lightPos[i][1] - 6 < 0)
                    minCol = 0;
                else
                    minCol = lightPos[i][1] - 6;
                if(lightPos[i][1] + 6 >= tiles[0].length)
                    maxCol = tiles[0].length - 1;
                else
                    maxCol = lightPos[i][1] + 6;
                for(int row = minRow; row <= maxRow; row++){
                    for(int col = minCol; col <= maxCol; col ++){
                        tilesTemp[row][col] = tiles[row][col];
                        map.setCell(col, row, tilesTemp[row][col]);
                        fixStateCell[row][col] = true;
                    }
                }
                // Xoa mep darkBorder khi bi lan anh sang
                for(int row = 0; row <= 8; row ++){
                    if(minCol - 1 >=0 && row + minRow <= tiles.length - 1){
                        if(tilesTemp[row + minRow][minCol - 1]!=0 && minCol - 1 < col_mr + 2){
                            darkBorder2.setCell(0, row, 0);
                        }
                    }
                    if(maxCol + 1 <=tiles[0].length - 1 && row + minRow <= tiles.length - 1){
                        if(tilesTemp[row + minRow][maxCol + 1]!=0 && maxCol > col_mr - 2)
                            darkBorder2.setCell(12, row, 0);
                    }
                }
                for(int col = 0; col <=12; col++){
                    if(minRow - 1 > 0 && col + minCol <= tiles[0].length - 1){
                        if(tilesTemp[minRow - 1][col + minCol]!=0 && minRow < row_mr + 4)
                            darkBorder2.setCell(col, 0, 0);
                    }
                    if(maxRow + 1 <= tiles.length - 1 && col + minCol <=tiles[0].length - 1){
                        if(tilesTemp[maxRow + 1][col + minCol]!=0 && maxRow > row_mr - 2){
                            darkBorder2.setCell(col, 8, 0);
                        }
                    }
                }
                //darkBorder2.setPosition(minCol * 16 - (mrLX - width/2), minRow * 16 - (mrLY - height/2));
                //darkBorder2.paint(g);
                darkBorder2.setPosition(minCol * 16 , minRow * 16 );
                gameCanvas.getLayerManager().insert(darkBorder2, 12);
            }
        }
        for(int row = 0; row < tiles.length; row++){
            for(int col = 0; col < tiles[0].length; col ++){
                if(tilesTemp[row][col] != 0 && !fixStateCell[row][col]){
                    tilesTemp[row][col] = 0;
                    map.setCell(col, row, tilesTemp[row][col]);
                }
            }
        }
        if(col_mr + 2 >= tiles[0].length)
            maxCol = tiles[0].length - 1;
        else
            maxCol = col_mr + 2;
        if(col_mr - 1 < 0)
            minCol = 0;
        else
            minCol = col_mr - 1;
        if(row_mr - 1 < 0)
            minRow = 0;
        else
            minRow = row_mr - 1;
        if(row_mr + 3 >= tiles.length)
            maxRow = tiles.length - 1;
        else
            maxRow = row_mr + 3;
        /*------ xoa phan bong toi bi che tren map khi L di chuyen ---------*/

        for(int row = minRow; row <= maxRow; row++){
            for(int col = minCol; col <= maxCol ; col ++){
                if(tilesTemp[row][col] == 0){
                    tilesTemp[row][col] = tiles[row][col];
                    map.setCell(col, row, tilesTemp[row][col]);
                }
            }
        }

        // Tao darkBorder tuong ung voi vien cua bong toi duoc xoa
        for (int row = 0; row < 5; row++) {
            for (int col = 0; col < 4; col++) {
                darkBorder.setCell(col, row, walkArray[row][col]);

            }
        }
        // Xoa mep darkBorder khi bi lan anh sang

        for(int col = 0; col <= 3; col ++){
            if(row_mr - 2 > 0){
                 for(int i = 0; i < lightPos.length; i++){
                     if((row_mr - 2 == lightPos[i][0] + 5) && row_mr <= lightPos[i][0] + 7 && row_mr >= lightPos[i][0] - 7 && col_mr <= lightPos[i][1] + 8 && col_mr >= lightPos[i][1] - 9){
                         isDelBorder = false;
                         break;
                     }
                     else
                         isDelBorder = true;
                }
                if(fixStateCell[row_mr - 2][col_mr - 1 + col] && isDelBorder){
                    darkBorder.setCell(col, 0, 0);
                }
            }
            if(row_mr + 4 < tiles.length){
                if(fixStateCell[row_mr + 4][col_mr - 1 + col] && row_mr + 4 != lightPos[1][0] - 3){
                    darkBorder.setCell(col, 4, 0);
                }
            }
        }
        for(int row = 0; row <= 4; row++){
            if(col_mr - 2 > 0){
                if(fixStateCell[row_mr - 1 + row][col_mr - 2] && col_mr - 2 != lightPos[1][1] + 6)
                    darkBorder.setCell(0, row, 0);
            }
            if(col_mr + 3 < tiles[0].length){
                for(int i = 0; i < lightPos.length; i++){
                     if((col_mr + 3 == lightPos[i][1]  - 6) && row_mr <= lightPos[i][0] + 7 && row_mr >= lightPos[i][0] - 7 && col_mr <= lightPos[i][1] + 8 && col_mr >= lightPos[i][1] - 9){
                         isDelBorder = false;
                         break;
                     }
                     else{
                         isDelBorder = true;
                     }
                }
                if(fixStateCell[row_mr - 1 + row][col_mr +  3] && isDelBorder)
                    darkBorder.setCell(3, row, 0);
            }
        }
        darkBorder.setPosition((col_mr - 1) * 16, (row_mr - 1)* 16);
    }
    
    public void paintItemInMap(SunnetGameCanvas gameCanvas, Graphics g){
        mrLX = gameCanvas.getMrL().getX();
        mrLY = gameCanvas.getMrL().getY();
        int x_src = (minCol - 22) * 16 + 8;
        if(x_src < 0)
            x_src = 0;
        if(x_src > 64)
            x_src = 64;
        int wp = (maxCol - 22) * 16 + 8;
        if(wp + x_src > 64)
            wp = 64 - x_src;
        if(wp < 0)
            wp = 0;
        int hp = (maxRow - 17) * 16 + 8;
        if(hp > 32 && minRow - 17 <= 0)
            hp = 32;
        if(minRow - 17 >0 || hp < 0)
            hp = 0;
        //System.out.println(minCol + "/" + maxCol + "/" + x_src + "/" + wp + "/" + hp);
        g.drawRegion(cars3IMG, x_src, 0, wp, hp, Sprite.TRANS_NONE, 22 * 16 - (mrLX - width /2) + x_src, 17 * 16 - (mrLY - height/2), 0);
        hp = (maxRow - 19) * 16 + 8;
        if(hp > 32 && minRow - 19 <= 0)
            hp = 32;
        if(minRow - 19 > 0 || hp < 0)
            hp = 0;
        g.drawRegion(cars3IMG, x_src, 0, wp, hp, Sprite.TRANS_NONE, 22 * 16 - (mrLX - width /2) + x_src, 19 * 16 - (mrLY - height/2), 0);
        hp = (maxRow - 21) * 16 + 8;
        if(hp > 32)
            hp = 32;
        if(hp < 0)
            hp = 0;
        g.drawRegion(cars3IMG, x_src, 0, wp, hp, Sprite.TRANS_NONE, 22 * 16 - (mrLX - width /2) + x_src, 21 * 16 - (mrLY - height/2), 0);
       
        if(tilesTemp[15][28]!=0 && !gameCanvas.updateToBag[9]){
            hieuung.setPosition(28 * 16 - 8 - (mrLX - width / 2), 14 * 16 - 8 - ((mrLY - height/2)));
            hieuung.nextFrame();
            hieuung.paint(g);
            gameCanvas.getItem().setFrame(14);
            gameCanvas.getItem().setPosition(28 * 16 - (mrLX - width/2),  14 * 16 - (mrLY - height/2));
            gameCanvas.getItem().paint(g);

        }
        // paint book
        if(tilesTemp[8][16] != 0 && tilesTemp[8][17]!=0 && !gameCanvas.updateToBag[8]){
            book.setVisible(true);
            hieuung.setPosition(16 * 16 - (mrLX - width / 2) , 7 * 16 - (mrLY - height / 2));
            hieuung.nextFrame();
            hieuung.paint(g);
        }
        else if(!gameCanvas.updateToBag[8]){
            book.setVisible(false);
        }
        //paint nuoc hoa
        if(tilesTemp[5][3] != 0 && !gameCanvas.updateToBag[7]){
            hieuung.setPosition(3 * 16 - 12 - (mrLX - width / 2), 5 * 16 - 8 - (mrLY - height / 2));
            hieuung.nextFrame();
            hieuung.paint(g);
            gameCanvas.getItem().setFrame(6);
            gameCanvas.getItem().setPosition(3 * 16 - (mrLX - width / 2) - 4, 5 * 16 - (mrLY - height / 2));
            gameCanvas.getItem().paint(g);
        }
        //paint belt
        if(tilesTemp[6][3]!= 0){
            gameCanvas.getItem().setFrame(1);
            gameCanvas.getItem().setPosition(3 * 16 - (mrLX - width / 2) - 4, 6 * 16 - (mrLY - height / 2));
            gameCanvas.getItem().paint(g);
        }
        if(tilesTemp[7][3]!= 0 && !gameCanvas.updateToBag[5]){
            if(gameCanvas.useItem[11]){
                hieuung.setPosition(3 * 16 - 12 - (mrLX - width / 2), 7 * 16 - 8 - (mrLY - height / 2));
                hieuung.nextFrame();
                hieuung.paint(g);
            }
            gameCanvas.getItem().setPosition(3 * 16 - (mrLX - width / 2) - 4, 7 * 16 - (mrLY - height / 2));
            gameCanvas.getItem().paint(g);
        }
        if(tilesTemp[8][3]!=0){
            gameCanvas.getItem().setPosition(3 * 16 - (mrLX - width / 2) - 4, 8 * 16 - (mrLY - height / 2));
            gameCanvas.getItem().paint(g);
        }    
        // paint tu do
        if(tilesTemp[(tudo1.getY() + tudo1.getHeight()) / 16][tudo1.getX() / 16] !=0){
            tudo1.setVisible(true);
        }
        else{
            tudo1.setVisible(false);
        }
        if(tilesTemp[(tudo12.getY() + tudo12.getHeight()) / 16][tudo12.getX() / 16]!=0){
            tudo12.setVisible(true);
            if(!gameCanvas.updateToBag[3]){
                hieuung.setPosition(5 * 16 + 6  - (mrLX - width / 2), 2 * 15 + 8 - (mrLY - height / 2));
                hieuung.nextFrame();
                hieuung.paint(g);
                gameCanvas.getItem().setFrame(2);
                gameCanvas.getItem().setPosition(tudo12.getX() - (mrLX - width / 2), tudo12.getY() - (mrLY - height / 2) - 10);
                gameCanvas.getItem().paint(g);
            }
        }
        else{
            tudo12.setVisible(false);
        }
        if(tilesTemp[(tudo2.getY() + tudo2.getHeight()) / 16 - 1][tudo2.getX() / 16] !=0){
            tudo2.setVisible(true);
        }
        else{
            tudo2.setVisible(false);
        }
        //paint ketsat
        
        if(tilesTemp[9][28] != 0){
            if(!gameCanvas.updateToBag[11]){
                hieuung.setPosition(27 * 16 + 4 - (mrLX - width / 2), 6 * 16 - (mrLY - height / 2));
                hieuung.nextFrame();
                hieuung.paint(g);
                gameCanvas.getItem().setFrame(7);
                gameCanvas.getItem().setPosition(ketsat.getX() - (mrLX - width / 2), ketsat.getY() - (mrLY - height / 2) - 10);
                gameCanvas.getItem().paint(g);
            }
            ketsat.setVisible(true);
        }
        else{
            ketsat.setVisible(false);
        }

        //paint cay
        if(tilesTemp[(caycanh.getY() + caycanh.getHeight()) / 16][caycanh.getX() / 16]!= 0){
            caycanh.setVisible(true);
        }
        else{
            caycanh.setVisible(false);
        }
        //paint monoquin
        if(tilesTemp[9][3] !=0){
            manoquin.setVisible(true);
        }
        else{
            manoquin.setVisible(false);
        }
        //paint button
        //loi qua
        button.setPosition(26 * 16 + 4, 6 * 16 + 4);
        if(fixStateCell[6][26]){
            button.setVisible(true);
            if(numItem == 11){
                if(!openDoor){
                    hieuung.setPosition(26 * 16 - 10 - (mrLX - width / 2), 6 * 16 - 10 - (mrLY - height / 2));
                    hieuung.nextFrame();
                    hieuung.paint(g);
                }
            }
        }
        else
            button.setVisible(false);

        //paint den
        for(int i = 0; i < lightPos.length; i ++){
            dento = (Sprite)light.elementAt(i);
            if(tilesTemp[lightPos[i][0] + 1][lightPos[i][1]]!=0 && tilesTemp[lightPos[i][0] + 2][lightPos[i][1] + 1]!=0){
                dento.setVisible(true);
            }
            else{
                dento.setVisible(false);
            }
        }

    }
    public void useItemInTuido(Graphics g, SunnetGameCanvas gameCanvas){
        if(gameCanvas.useItem[8]){
            if(book.getFrame() == 0){
                talk = string[9];
            }
            if(!nextString){
                if(endTextID == talk.length()){
                    nextString = true;
                    talk = string[10];
                    isDrawing = false;
                }
                
            }
            delay = true;
            if(nextFrame % 2 == 0){
                if(book.getFrame() !=7)
                    book.nextFrame();
            }
            if(book.getFrame() == 7){
                showBook = true;
            }
            book.setPosition(width / 2 - 60, height / 2 - 60);
            book.paint(g);
        }
        if(gameCanvas.useItem[10]){   
            talk = string[8];
            delay = true;
            if(nextFrame % 2 == 0){
                if(paper.getFrame() != 4)
                    paper.nextFrame();
            }
            if(paper.getFrame() == 4)
                showPaper = true;
            paper.setPosition(width / 2 - 25, height / 2 - 20);
            paper.paint(g);
        }
    }
    public void freeMemory(){
        tilesTemp = null; mapXY = null; tilesDarkDeleteByLight = null;
        walkArray = null; tiles = null; button = null;
        
        tudo1 = null; tudo12 = null; tudo2 = null; manoquin = null; bangdientu = null;
        dento = null; light = null; book = null; paper = null; ketsat = null; caycanh = null;
        darkBorder = null; map = null; darkBorder2 = null; hieuung = null;

        tudo1IMG = null; tudo2IMG = null; cars3IMG = null; nhakhoIMG = null;
        caycanhIMG = null; paperIMG = null; bookIMG = null;
        darkBorderIMG = null; dentoIMG = null; bangdientuIMG = null;
        ketsatIMG = null; manoquinIMG = null; buttonIMG = null;
    }
    public void talkProcess(Graphics g, SunnetGameCanvas gameCanvas){
        if(delay){
            g.setColor(55, 77, 91);
            g.fillRect(20, height - 90, 200, 60);
            gameCanvas.getMatNV().setFrame(4);        
            gameCanvas.getMatNV().setPosition(25, height - 85);
            gameCanvas.getMatNV().paint(g);
            gameCanvas.getKhungThoai().setPosition(20, height - 90);
            gameCanvas.getKhungThoai().paint(g); 
            if(!isDrawing){
                textT = new byte[talk.length()];     
                Designer.toBytesIndex(talk , textT);
                isDrawing = true;
                endTextID = 0;
            }
            else{
                if(endTextID < talk.length()) 
                    endTextID ++;
                else{
                    if(!gameCanvas.useItem[10] && !gameCanvas.useItem[8]){
                        delay = false;
                        isDrawing = false;
                        if(nextString){
                            nextString = false;
                            endString = true;
                        }
                    }
                    
                    if(gameCanvas.getMrL().collidesWith(tudo1, true)){
                        if(tudo1.getFrame() == 0){
                            open = true;        
                            tudo1.nextFrame();
                        }        
                        if(tudo1.getFrame() == 5){
                            
                            tudo1.nextFrame();
                            gameCanvas.updateToTuido(2);
                            numItem ++;
                        }
                    }
          
                    if(gameCanvas.getMrL().collidesWith(ketsat, true)){
                        textForBoard = true;      
                        if(ketsat.getFrame() == 3){
                            ketsat.nextFrame();
                            gameCanvas.updateToTuido(12);
                            numItem ++;
                        }
                    }
                }
                Designer.drawString(g, textT, 0, endTextID, Designer.FONT_TEXT, Designer.MARGIN_LEFT, false, 2, 75 , height - 85, 150, 50);
            }
        }
    }
    public void drawMap(Graphics g, SunnetGameCanvas gameCanvas){
        mrLX = gameCanvas.getMrL().getX();
        mrLY = gameCanvas.getMrL().getY();
        gameCanvas.getLayerManager().setViewWindow(mrLX - width / 2, mrLY - height/2, width, height);
        gameCanvas.getLayerManager().paint(g, 0, 0);
        paintItemInMap(gameCanvas, g);
        row_mr = mrLY / 16;
        col_mr = mrLX / 16;
        deleteDark(gameCanvas, g);
        if(openDoor){
            button.nextFrame();
            openDoorProcees();
        }
        if(!boardApp){
            useItemInTuido(g, gameCanvas);
        }

        else if(boardApp){
            g.setColor(0x000000);
            g.fillRect(0, 0, width, height);
            bangdientu.setPosition(width/2 - 60, height/2 - 60);
            bangdientu.paint(g);
            
            byte[] text = new byte[5];
            Designer.toBytesIndex("Thoát", text);
            Designer.drawString(g,text, 0, 5, Designer.FONT_TEXT, 1, width - 35 , height - 15);

            if(drawOutputText){
                text = new byte[output.length()];
                Designer.toBytesIndex(output, text);
                if(nextFrame % 5 == 0)
                    Designer.drawString(g,text, 0,output.length(), Designer.FONT_TEXT, 1, (bangdientu.getX() + 11) , (bangdientu.getY() + 10));
                        
            }
            g.setColor(255, 0, 0);
            if(char_row < 3)
                g.drawRect((bangdientu.getX()  + 6) + char_col * 12 , (bangdientu.getY() + 46) + char_row * 16 , 11, 11);
            else
                g.drawRect((bangdientu.getX()  + 11) + char_col * 50 , (bangdientu.getY() + 46) + char_row * 16 , 47, 11);
            if(stringCp.length() !=0){
                text = new byte[stringCp.length()];
                Designer.toBytesIndex(stringCp, text);
                Designer.drawString(g,text, 0, stringCp.length(), Designer.FONT_TEXT, 1, (bangdientu.getX() + 11) , (bangdientu.getY() + 10));
            }
            if(gameCanvas.nextScence){
                g.setColor(0x000000);
                g.fillRect(0, height/2 + gameCanvas.rh, width, height/2);
                g.fillRect(0, 0, width, height/2 - gameCanvas.rh);
            }
        }
        talkProcess(g, gameCanvas);
        
    }
}


